// 文件位置: src/main/java/your/package/controller/ParkingSpotController.java
package your.package.controller;

import your.package.model.ParkingSpot;
import your.package.repository.ParkingSpotRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/parking-spots")
public class ParkingSpotController {

    @Autowired
    private ParkingSpotRepository parkingSpotRepository;

    @GetMapping("/")
    public List<ParkingSpot> getAllParkingSpots() {
        return parkingSpotRepository.findAll();
    }

    @PostMapping("/")
    public ParkingSpot createParkingSpot(@RequestBody ParkingSpot parkingSpot) {
        return parkingSpotRepository.save(parkingSpot);
    }

    @PutMapping("/{id}")
    public ParkingSpot updateParkingSpot(@PathVariable Long id, @RequestBody ParkingSpot spotDetails) {
        ParkingSpot parkingSpot = parkingSpotRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Parking Spot not found on :: " + id));
        parkingSpot.setLocation(spotDetails.getLocation());
        parkingSpot.setIsOccupied(spotDetails.getIsOccupied());
        return parkingSpotRepository.save(parkingSpot);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteParkingSpot(@PathVariable Long id) {
        ParkingSpot parkingSpot = parkingSpotRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("Parking Spot not found on :: " + id));
        parkingSpotRepository.delete(parkingSpot);
        return ResponseEntity.ok().build();
    }
}
